<?php
class TE extends TEParser{

	public $mediators = array();
	
	protected $cache = null;
	
	public function setCache($cache){
		$this->cache = $cache;
		return $this;
	}

	public function parse2($text){
		if(isset($this->cache)){
			
			$key = md5($text.json_encode($this->getProperties()).serialize($_REQUEST).serialize($_SERVER['QUERY_STRING']).serialize($_SERVER['REQUEST_URI']));
			$e=$this->cache->get($key);
			if(!empty($e)){
				#echo "<script>alert('CACHE')</script>";
				return gzuncompress($e);
			}else{
				#echo "<script>alert('<pre>".json_encode($_SERVER)."')</script>";
				$text = Parser::parse($text);
				$this->cache->set($key,gzcompress($text, 1));
				return $text;
			}
		}else{
			return Parser::parse($text);
		}
	}
	
	public function addMediator($mediator){
		
		$class = get_class($mediator); //Solo un mediator por clase?? Forza a desarrolla clases genericas?
		
		$this->mediators[$class] = & $mediator; //Toma la referencia del mediator
		
		$self = &$this;
		return $self;
	}
	
	public function hasMediator($name=0){
		return isset($this->mediators[$name]);
	}
	
	public function getMediator($name=0){
		return $this->mediators[$name];
	}
	public function getChilds($name){
		return $this->objectChilds[$name];
	}
	public function getParent(){
		return $this->objectParent;
	}
	
	public function set($property,$value=""){
		$this->$property = $value;
		return $this;
	}
	
	public function setObject($object){
		foreach(get_object_vars($object) as $property => $value ){
			if(is_scalar($value)){
				$this->$property = (string)$value;
			}
		}
	}
	
	public function getObject(){
		return (object) $this->getProperties();
	}
	
	public function getListOfProperties(){
		$list = array();
		
		foreach(array_keys(get_class_vars(get_class($this))) as $property){
			$class = get_class($this);
			if(isset($class::$$property)){
					$list[$property] = $this->$property; 
			}
		}
		if(isset($this->properties))
		foreach($this->properties as $property){
			if(isset($this->$property)){
				$list[$property] = $this->$property;
			}
		}

		return $list;	
	}
	
	public function getProperties(){
		$result = array();
		
		foreach($this->getListOfProperties() as $property){
			if(isset($this->$property)){
				$result[$property] = $this->$property;
			}	
		}
		return $result;
	}
	
		
	public function get($property){
		return $this->$property;
	}
}
?>